import os
import platform
import subprocess
import time

import pyautogui
import pyperclip


def install_wmctrl():
    """检查并安装wmctrl"""
    try:
        subprocess.check_call(['which', 'wmctrl' ,'xclip'])
    except subprocess.CalledProcessError:
        print("请安装wmctrl和xclip...")


def wait_for_wechat():
    """等待微信窗口出现"""
    print("等待微信登录...")
    while True:
        try:
            output = subprocess.check_output(['wmctrl', '-l']).decode()
            if '微信' in output:
                print("检测到微信窗口")
                return
            time.sleep(5)
        except subprocess.CalledProcessError:
            time.sleep(1)


def setup_window():
    """调整微信窗口位置和尺寸"""
    # 激活微信窗口、设置窗口位置和尺寸（根据实际需求调整）
    # 格式：gravity,X,Y,width,height
    subprocess.run(['wmctrl','-a', '微信'])
    subprocess.run(['wmctrl', '-r', '微信', '-e', '0,0,0,0,0'])
    time.sleep(1)


def send_message():
    """模拟输入并发送消息"""
    # 搜索联系人
    # 输入框坐标（根据实际窗口调整）
    input_x1 = 5
    input_y1 = 5
    # 点击输入框
    pyautogui.click(input_x1, input_y1)
    pyautogui.hotkey('ctrl', 'f')

    # 清理搜索框内容
    pyautogui.hotkey('ctrl', 'a')
    pyautogui.hotkey('ctrl', 'x')
    time.sleep(0.5)

    # 消息内容
    pyperclip.copy("文件传输助手")
    pyautogui.hotkey('ctrl', 'v')
    time.sleep(1)
    pyautogui.press('enter')
    time.sleep(1)

    # 输入框坐标（根据实际窗口调整）
    input_x = 380
    input_y = 670
    # 点击输入框
    pyautogui.click(input_x, input_y)
    time.sleep(0.5)
    # 输入内容（支持中文）
    message="发送内容1111"
    pyperclip.copy(message)
    pyautogui.hotkey('ctrl', 'v')
    time.sleep(0.5)

    # 发送消息
    pyautogui.press('enter')


if __name__ == "__main__":
    install_wmctrl()
    wait_for_wechat()
    setup_window()
    send_message()
    print("消息发送成功！")